﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessObjectLayer;
using BusinessLogicLayer;
using System.Resources;
using System.Reflection;
using JntMsgBox;

namespace UserInterface
{

    public partial class MenuUI : Form
    {
        bool closingFlag = false;
        ResourceManager _ResourceManager = new ResourceManager("UserInterface.Properties.Resources", Assembly.GetExecutingAssembly());
        ChangePasswordUI _ChangePasswordUI;
        ShowUserUI _ShowUserUI;
        ShowPaperUI _ShowPaperUI;
        ShowHelpUI _ShowHelpUI;
        ShowGalleryUI _ShowGalleryUI;  
        About _About;
        BackupUI _BackupUI;
        RestoreBackupUI _RestoreBackupUI;
        SentenceUI _SentenceUI;
        ShowCategoryUI _ShowCategoryUI;
        ShowCategoryItemUI _ShowCategoryItemUI;
        ShowSettingUI _ShowSettingUI; 
        ExceptionHandlerBOL _ExceptionHandlerBOL;
        string ExitText;
        string ChangeText;
        string BackupText;
        string ExitConfirmation;
        string ChangeConfirmation;
        string BackupConfirmation;
        private AccessMode accessMode = AccessMode.NoAccess;

        public MenuUI()
        {
            InitializeComponent();
            this.Text = string.Format("{0} نسخه {1}",this.Text,Application.ProductVersion.Substring(0,Application.ProductVersion.IndexOf(".")));
            CreateVisualStyleColors();
            SetResourceManager();
            ShowDate();
            this.LayoutMdi(MdiLayout.ArrangeIcons);
        }
        private void ShowDate()
        {
            dateLabel.Text = DataConvertor.DataConvertor.ConvertToPersianDate(DateTime.Now);
        }
     
        private void SetResourceManager()
        {
            ExitText = _ResourceManager.GetString("Exit");
            ChangeText = _ResourceManager.GetString("Change");
            BackupText = _ResourceManager.GetString("Backup");
            ExitConfirmation = _ResourceManager.GetString("ExitConfirmation");
            ChangeConfirmation = _ResourceManager.GetString("ChangeConfirmation");
            BackupConfirmation = _ResourceManager.GetString("BackupConfirmation");
        }
        private void CreateVisualStyleColors()
        {
            this.CreateBuiltInVisualStyleCommand("rcmdBlue", "Blue", Color.FromArgb(118, 153, 199));
            this.CreateBuiltInVisualStyleCommand("rcmdSilver", "Silver", Color.Silver);
            this.CreateBuiltInVisualStyleCommand("rcmdBlack", "Black", Color.Black);
            Janus.Windows.Ribbon.ButtonCommand cmdBlue = this.officeColorsRibbonGroup.Commands["rcmdBlue"] as Janus.Windows.Ribbon.ButtonCommand;
            cmdBlue.Checked = true;

            colorsGallery.MaxGalleryColumns = 6;
            AddCustomColorCommand(Color.FromArgb(96, 128, 160), "21");
            AddCustomColorCommand(Color.FromArgb(160, 96, 96), "22");
            AddCustomColorCommand(Color.FromArgb(128, 160, 96), "23");
            AddCustomColorCommand(Color.FromArgb(96, 160, 128), "24");
            AddCustomColorCommand(Color.FromArgb(128, 128, 160), "25");
            AddCustomColorCommand(Color.FromArgb(160, 96, 128), "26");

            AddCustomColorCommand(Color.FromArgb(80, 128, 192), "21");
            AddCustomColorCommand(Color.FromArgb(192, 80, 80), "22");
            AddCustomColorCommand(Color.FromArgb(128, 192, 80), "23");
            AddCustomColorCommand(Color.FromArgb(80, 192, 128), "24");
            AddCustomColorCommand(Color.FromArgb(128, 128, 192), "25");
            AddCustomColorCommand(Color.FromArgb(192, 80, 128), "26");

            AddCustomColorCommand(Color.FromArgb(40, 80, 128), "31");
            AddCustomColorCommand(Color.FromArgb(128, 40, 40), "32");
            AddCustomColorCommand(Color.FromArgb(80, 128, 40), "33");
            AddCustomColorCommand(Color.FromArgb(40, 128, 80), "45");
            AddCustomColorCommand(Color.FromArgb(80, 80, 128), "34");
            AddCustomColorCommand(Color.FromArgb(128, 40, 80), "34");

            AddCustomColorCommand(Color.FromArgb(32, 48, 80), "21");
            AddCustomColorCommand(Color.FromArgb(80, 40, 40), "22");
            AddCustomColorCommand(Color.FromArgb(48, 80, 32), "23");
            AddCustomColorCommand(Color.FromArgb(32, 80, 48), "24");
            AddCustomColorCommand(Color.FromArgb(40, 40, 80), "25");
            AddCustomColorCommand(Color.FromArgb(80, 32, 40), "26");

            AddCustomColorCommand(Color.FromArgb(24, 40, 64), "41");
            AddCustomColorCommand(Color.FromArgb(64, 24, 24), "42");
            AddCustomColorCommand(Color.FromArgb(40, 64, 32), "43");
            AddCustomColorCommand(Color.FromArgb(24, 64, 48), "45");
            AddCustomColorCommand(Color.FromArgb(32, 32, 64), "44");
            AddCustomColorCommand(Color.FromArgb(64, 24, 48), "44");
        }
        private void AddCustomColorCommand(Color clr, string name)
        {
            Janus.Windows.Ribbon.GalleryItem item = new Janus.Windows.Ribbon.GalleryItem("COLOR" + name);
            item.Tag = clr;
            item.Image = CreateColorImage(clr, 48, 48);
            this.colorsGallery.GalleryItems.Add(item);
        }
        private void MenuRibbon_CommandClick(object sender, Janus.Windows.Ribbon.CommandEventArgs e)
        {
            if (e.Command.Key.StartsWith("COLOR"))
            {
                Janus.Windows.Common.VisualStyleManager.DefaultOffice2007ColorScheme = Janus.Windows.Common.Office2007ColorScheme.Custom;
                Janus.Windows.Common.VisualStyleManager.DefaultOffice2007CustomColor = (Color)e.Command.Tag;
                foreach (Janus.Windows.Ribbon.ButtonCommand _ButtonCommand in this.officeColorsRibbonGroup.Commands)
                {
                    _ButtonCommand.Checked = false;
                }
            }
            else
            {
                switch (e.Command.Key)
                {
                    case "rcmdBlack":
                        Janus.Windows.Common.VisualStyleManager.DefaultOffice2007ColorScheme = Janus.Windows.Common.Office2007ColorScheme.Black;
                        UncheckCustomColors();
                        break;
                    case "rcmdBlue":
                        Janus.Windows.Common.VisualStyleManager.DefaultOffice2007ColorScheme = Janus.Windows.Common.Office2007ColorScheme.Blue;
                        UncheckCustomColors();
                        break;
                    case "rcmdSilver":
                        Janus.Windows.Common.VisualStyleManager.DefaultOffice2007ColorScheme = Janus.Windows.Common.Office2007ColorScheme.Silver;
                        UncheckCustomColors();
                        break;
                    case "changePasswordButtonCommand":
                        ChangePassword();
                        break;
                    case "changeUserButtonCommand":
                        RestartApplication();
                        break;
                    case "exitButtonCommand":
                        GetExitConfirmtion();
                        break;                   
                    case "usersButtonCommand":
                        ShowUserUI();
                        break;
                    case "paperButtonCommand":
                        ShowPaperUI();
                        break;
                    case "categoryButtonCommand":
                        ShowCategoryUI();
                        break;
                    case "categoryItemButtonCommand":
                        ShowCategoryItemUI();
                        break;
                    case "helpButtonCommand":
                        ShowHelpUI();
                        break;
                    case "galleryButtonCommand":
                        ShowGalleryUI();
                        break;
                    case "cascadeButtonCommand":
                        this.LayoutMdi(MdiLayout.Cascade);
                        break;
                    case "horizontalButtonCommand":
                        this.LayoutMdi(MdiLayout.TileHorizontal);
                        break;
                    case "verticalButtonCommand":
                        this.LayoutMdi(MdiLayout.TileVertical);
                        break;
                    case "restoreAllButtonCommand":
                        RestoreAll();
                        break;
                    case "closeAllButtonCommand":
                        CloseAll();
                        break;
                    case "minimizeAllButtonCommand":
                        MinimizeAll();
                        break;                  
                    case "aboutButtonCommand":
                        ShowAbout();
                        break;
                    case "backupButtonCommand":
                        ShowBackup();
                        break;
                    case "restoreBackupButtonCommand":
                        ShowRestoreBackup();
                        break;
                    case "sentenceButtonCommand":
                        ShowSentence();
                        break;
                    case "settingButtonCommand":
                        ShowSetting();
                        break;
                }
            }
        }
        private void ShowSetting()
        {
            try
            {
                if (_ShowSettingUI == null || !_ShowSettingUI.Visible)
                {
                    if (_ShowSettingUI != null)
                        _ShowSettingUI.Dispose();
                    accessMode = GetAccessMode("Setting");
                    if (accessMode == AccessMode.NoAccess)
                        return;
                    _ShowSettingUI = new ShowSettingUI(accessMode);
                    _ShowSettingUI.MdiParent = this;
                    _ShowSettingUI.Show();
                }
                else
                {
                    _ShowSettingUI.Focus();
                    _ShowSettingUI.WindowState = FormWindowState.Normal;
                }
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                errorLabelCommand.Text = _ExceptionHandlerBOL.Title;
                errorLabelCommand.DescriptionForegroundStyle.ForeColor = Color.Red;
            }
        }
        private void ShowGalleryUI()
        {
            try
            {
                if (_ShowGalleryUI == null || !_ShowGalleryUI.Visible)
                {
                    if (_ShowGalleryUI != null)
                        _ShowGalleryUI.Dispose();
                    accessMode = GetAccessMode("Gallery");
                    if (accessMode == AccessMode.NoAccess)
                        return;
                    _ShowGalleryUI = new ShowGalleryUI(accessMode);
                    _ShowGalleryUI.MdiParent = this;
                    _ShowGalleryUI.Show();
                }
                else
                {
                    _ShowGalleryUI.Focus();
                    _ShowGalleryUI.WindowState = FormWindowState.Normal;
                }
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                errorLabelCommand.Text = _ExceptionHandlerBOL.Title;
                errorLabelCommand.DescriptionForegroundStyle.ForeColor = Color.Red;
            }
        }
        private void ShowSentence()
        {
            try
            {
                if (_SentenceUI == null || !_SentenceUI.Visible)
                {
                    if (_SentenceUI != null)
                        _SentenceUI.Dispose();
                    accessMode = GetAccessMode("Sentence");
                    if (accessMode == AccessMode.NoAccess)
                        return;
                    _SentenceUI = new SentenceUI();
                    _SentenceUI.MdiParent = this;
                    _SentenceUI.Show();
                }
                else
                {
                    _SentenceUI.Focus();
                    _SentenceUI.WindowState = FormWindowState.Normal;
                }
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                errorLabelCommand.Text = _ExceptionHandlerBOL.Title;
                errorLabelCommand.DescriptionForegroundStyle.ForeColor = Color.Red;
            }
        }
        private void ShowRestoreBackup()
        {
            try
            {
                if (_RestoreBackupUI == null || !_RestoreBackupUI.Visible)
                {
                    if (_RestoreBackupUI != null)
                        _RestoreBackupUI.Dispose();
                    accessMode = GetAccessMode("RestoreBackup");
                    if (accessMode == AccessMode.NoAccess)
                        return;
                    _RestoreBackupUI = new RestoreBackupUI();
                    _RestoreBackupUI.MdiParent = this;
                    _RestoreBackupUI.Show();
                }
                else
                {
                    _RestoreBackupUI.Focus();
                    _RestoreBackupUI.WindowState = FormWindowState.Normal;
                }
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                errorLabelCommand.Text = _ExceptionHandlerBOL.Title;
                errorLabelCommand.DescriptionForegroundStyle.ForeColor = Color.Red;
            }
        }
        private void ShowBackup()
        {
            try
            {
                if (_BackupUI == null || !_BackupUI.Visible)
                {
                    if (_BackupUI != null)
                        _BackupUI.Dispose();
                    accessMode = GetAccessMode("Backup");
                    if (accessMode == AccessMode.NoAccess)
                        return;
                    _BackupUI = new BackupUI();
                    _BackupUI.MdiParent = this;
                    _BackupUI.Show();
                }
                else
                {
                    _BackupUI.Focus();
                    _BackupUI.WindowState = FormWindowState.Normal;
                }
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                errorLabelCommand.Text = _ExceptionHandlerBOL.Title;
                errorLabelCommand.DescriptionForegroundStyle.ForeColor = Color.Red;
            }
        }
        private void ShowAbout()
        {
            try
            {
                if (_About == null || !_About.Visible)
                {
                    if (_About != null)
                        _About.Dispose();
                    _About = new About();
                    _About.MdiParent = this;
                    _About.Show();
                }
                else
                {
                    _About.Focus();
                    _About.WindowState = FormWindowState.Normal;
                }
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                errorLabelCommand.Text = _ExceptionHandlerBOL.Title;
                errorLabelCommand.DescriptionForegroundStyle.ForeColor = Color.Red;
            }
        }     
        private void MinimizeAll()
        {
            foreach (Form childform in this.MdiChildren)
            {
                childform.WindowState = FormWindowState.Minimized;
            }
        }
        private void CloseAll()
        {
            foreach (Form childform in this.MdiChildren)
            {
                childform.Close();
            }
        }
        private void RestoreAll()
        {
            foreach (Form childform in this.MdiChildren)
            {
                childform.WindowState = FormWindowState.Normal;
            }
        }         
        private void ShowUserUI()
        {
            try
            {
                if (_ShowUserUI == null || !_ShowUserUI.Visible)
                {
                    if (_ShowUserUI != null)
                        _ShowUserUI.Dispose();
                    accessMode = GetAccessMode("ShowUser");
                    if (accessMode == AccessMode.NoAccess)
                        return;
                    _ShowUserUI = new ShowUserUI(accessMode);
                    _ShowUserUI.MdiParent = this;
                    _ShowUserUI.Show();
                }
                else
                {
                    _ShowUserUI.Focus();
                    _ShowUserUI.WindowState = FormWindowState.Normal;
                }
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                errorLabelCommand.Text = _ExceptionHandlerBOL.Title;
                errorLabelCommand.DescriptionForegroundStyle.ForeColor = Color.Red;
            }
        }
        private void ShowPaperUI()
        {
            try
            {
                if (_ShowPaperUI == null || !_ShowPaperUI.Visible)
                {
                    if (_ShowPaperUI != null)
                        _ShowPaperUI.Dispose();
                    accessMode = GetAccessMode("ShowPaper");
                    if (accessMode == AccessMode.NoAccess)
                        return;
                    _ShowPaperUI = new ShowPaperUI(accessMode);
                    _ShowPaperUI.MdiParent = this;
                    _ShowPaperUI.Show();
                }
                else
                {
                    _ShowPaperUI.Focus();
                    _ShowPaperUI.WindowState = FormWindowState.Normal;
                }
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                errorLabelCommand.Text = _ExceptionHandlerBOL.Title;
                errorLabelCommand.DescriptionForegroundStyle.ForeColor = Color.Red;
            }
        }
        private void ShowHelpUI()
        {
            try
            {
                if (_ShowHelpUI == null || !_ShowHelpUI.Visible)
                {
                    if (_ShowHelpUI != null)
                        _ShowHelpUI.Dispose();
                    accessMode = GetAccessMode("ShowHelp");
                    if (accessMode == AccessMode.NoAccess)
                        return;
                    _ShowHelpUI = new ShowHelpUI(accessMode);
                    _ShowHelpUI.MdiParent = this;
                    _ShowHelpUI.Show();
                }
                else
                {
                    _ShowHelpUI.Focus();
                    _ShowHelpUI.WindowState = FormWindowState.Normal;
                }
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                errorLabelCommand.Text = _ExceptionHandlerBOL.Title;
                errorLabelCommand.DescriptionForegroundStyle.ForeColor = Color.Red;
            }
        }
        private void ShowCategoryUI()
        {
            try
            {
                if (_ShowCategoryUI == null || !_ShowCategoryUI.Visible)
                {
                    if (_ShowCategoryUI != null)
                        _ShowCategoryUI.Dispose();
                    accessMode = GetAccessMode("ShowCategory");
                    if (accessMode == AccessMode.NoAccess)
                        return;
                    _ShowCategoryUI = new ShowCategoryUI(accessMode);
                    _ShowCategoryUI.MdiParent = this;
                    _ShowCategoryUI.Show();
                }
                else
                {
                    _ShowCategoryUI.Focus();
                    _ShowCategoryUI.WindowState = FormWindowState.Normal;
                }
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                errorLabelCommand.Text = _ExceptionHandlerBOL.Title;
                errorLabelCommand.DescriptionForegroundStyle.ForeColor = Color.Red;
            }
        }
        private void ShowCategoryItemUI()
        {
            try
            {
                if (_ShowCategoryItemUI == null || !_ShowCategoryItemUI.Visible)
                {
                    if (_ShowCategoryItemUI != null)
                        _ShowCategoryItemUI.Dispose();
                    accessMode = GetAccessMode("ShowCategoryItem");
                    if (accessMode == AccessMode.NoAccess)
                        return;
                    _ShowCategoryItemUI = new ShowCategoryItemUI(accessMode);
                    _ShowCategoryItemUI.MdiParent = this;
                    _ShowCategoryItemUI.Show();
                }
                else
                {
                    _ShowCategoryItemUI.Focus();
                    _ShowCategoryItemUI.WindowState = FormWindowState.Normal;
                }
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                errorLabelCommand.Text = _ExceptionHandlerBOL.Title;
                errorLabelCommand.DescriptionForegroundStyle.ForeColor = Color.Red;
            }
        }
        private AccessMode GetAccessMode(string pageTitle)
        {
            errorLabelCommand.Text = string.Empty;
            AccessMode accessMode = AccessMode.Complete;//AccessBLL.GetAccessMode(pageTitle, UserProfile.RoleId);
            if (accessMode == AccessMode.NoAccess)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(new Exception("NoAccess"));
                errorLabelCommand.DescriptionForegroundStyle.ForeColor = Color.Red;
                errorLabelCommand.Text = _ExceptionHandlerBOL.Title;
            }
            return accessMode;
        }
        private void GetExitConfirmtion()
        {                                
            if (closingFlag == false)
            {
                JntMsgBoxFarsi _JntMsgBoxFarsi = new JntMsgBoxFarsi();
                DialogResult result = _JntMsgBoxFarsi.Show(BackupConfirmation, BackupText, JntStyle.YesNo);
                if (result == DialogResult.Yes)
                    ShowBackup();
                else
                {
                    closingFlag = true;
                    result = _JntMsgBoxFarsi.Show(ExitConfirmation, ExitText, JntStyle.YesNo);
                    if (result == DialogResult.Yes)
                        CloseForm();
                    else
                        closingFlag = false;
                }
            }
        }
        private void CloseForm()
        {
            this.Close();
        }
        private void CloseApplication()
        {
            Application.Exit();
        }
        private void RestartApplication()
        {
            closingFlag = true;
            GetChangeConfirmtion();
        }
        private void GetChangeConfirmtion()
        {
            JntMsgBoxFarsi _JntMsgBoxFarsi = new JntMsgBoxFarsi();
            DialogResult result = _JntMsgBoxFarsi.Show(ChangeConfirmation, ChangeText, JntStyle.YesNo);
            if (result == DialogResult.Yes)
                Application.Restart();
            closingFlag = false;
        }
        private void ChangePassword()
        {
            try
            {
                if (_ChangePasswordUI == null || !_ChangePasswordUI.Visible)
                {
                    if (_ChangePasswordUI != null)
                        _ChangePasswordUI.Dispose();
                    _ChangePasswordUI = new ChangePasswordUI();
                    _ChangePasswordUI.Show();
                }
                else
                {
                    _ChangePasswordUI.Focus();
                    _ChangePasswordUI.WindowState = FormWindowState.Normal;
                }
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                errorLabelCommand.Text = _ExceptionHandlerBOL.Title;
                errorLabelCommand.DescriptionForegroundStyle.ForeColor = Color.Red;
            }
        }
        private void UncheckCustomColors()
        {
            foreach (Janus.Windows.Ribbon.GalleryItem item in this.colorsGallery.GalleryItems)
            {
                item.Checked = false;
            }
        }
        private void CreateBuiltInVisualStyleCommand(string key, string text, Color imageColor)
        {
            Janus.Windows.Ribbon.ButtonCommand command = new Janus.Windows.Ribbon.ButtonCommand(key);
            command.SizeStyle = Janus.Windows.Ribbon.CommandSizeStyle.Large;
            command.ActAsOptionButton = true;
            command.CheckOnClick = true;
            command.Text = text;
            command.Image = CreateColorImage(imageColor, 16, 16);
            command.LargeImage = CreateColorImage(imageColor, 32, 32);
            this.officeColorsRibbonGroup.Commands.Add(command);
        }
        private Image CreateColorImage(Color clr, int width, int height)
        {
            Bitmap bmp = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(clr);
            Rectangle r = new Rectangle(0, 0, width - 1, height - 1);
            g.DrawRectangle(Pens.Black, r);
            r.Inflate(-1, -1);
            g.DrawRectangle(Pens.White, r);
            g.Dispose();
            return bmp;
        }
        private void MenuUI_FormClosing(object sender, FormClosingEventArgs e)
        {
            GetExitConfirmtion();            
            if (!closingFlag)
                e.Cancel = true;
            RemoveRibbonDynamicCommands();
        }
        private void RemoveRibbonDynamicCommands()
        {
            this.officeColorsRibbonGroup.Commands.Clear();
            this.colorsGallery.GalleryItems.Clear();
        }
        private void errorLabelCommand_Click(object sender, Janus.Windows.Ribbon.CommandEventArgs e)
        {
            if (_ExceptionHandlerBOL != null)
            {
                new ExceptionHandlerUI(_ExceptionHandlerBOL).ShowDialog();
                errorLabelCommand.Text = string.Empty;
            }
            _ExceptionHandlerBOL = null;
        }
        private void MenuUI_FormClosed(object sender, FormClosedEventArgs e)
        {
            CloseApplication();
        }        
        private void paperLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //ShowPaperUI();
        }
    }
}
